package com.hw.rediscache.service;

import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: spring-data-redis
 * @description:
 * @author: hewei
 * @date: 2021-10-28 13:36
 **/
@Service
@CacheConfig(cacheNames = {"usersCache"},keyGenerator = "cacheKeyGenerator")
public class RedisCacheService {

    private static List<String> nameList;

    public RedisCacheService() {
        nameList = new ArrayList<>();
        nameList.add("杨萍");
        nameList.add("马国萍");
        nameList.add("马青青");
        nameList.add("杨溢春");
        nameList.add("张港欣");
        nameList.add("杨丹");
    }

//    @Cacheable(value = "users",keyGenerator = "cacheKeyGenerator")
    @Cacheable
    public List<String> getInfoList(String name){
        List<String> result=new LinkedList<>();
        result=nameList.stream().filter(m->
            m.contains(name)
        ).collect(Collectors.toList());
        return result;
    }

    @CachePut
    public void addName(String name){
        nameList.add(name);
    }
}
